/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.oa.service;

import java.util.List;
import java.util.Map;

import org.activiti.engine.IdentityService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Maps;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.act.utils.ActUtils;
import com.jeeplus.modules.oa.entity.Overtime;
import com.jeeplus.modules.oa.mapper.OvertimeMapper;

/**
 * 加班表Service
 * @author 林鹏群
 * @version 2017-07-25
 */
@Service
@Transactional(readOnly = true)
public class OvertimeService extends CrudService<OvertimeMapper, Overtime> {

	@Autowired
	private ActTaskService actTaskService;
	
	@Autowired
	private IdentityService identityService;
	
	public Overtime getByProcInsId(String procInsId) {
		return mapper.getByProcInsId(procInsId);
	}
	
	
	/**
	 * 审核新增或编辑
	 * @param t
	 */
	@Transactional(readOnly = false)
	public void save(Overtime overtime) {
		
		// 申请发起
		if (StringUtils.isBlank(overtime.getId())){
			overtime.preInsert();
			mapper.insert(overtime);
			
			
			// 用来设置启动流程的人员ID，引擎会自动把用户ID保存到activiti:initiator中
			identityService.setAuthenticatedUserId(overtime.getCurrentUser().getLoginName());
			// 启动流程
			actTaskService.startProcess(ActUtils.PD_OVERTIME[0], ActUtils.PD_OVERTIME[1], overtime.getId(), overtime.getContent());
			
		}
		
		// 重新编辑申请		
		else{
			overtime.preUpdate();
			mapper.update(overtime);

			overtime.getAct().setComment(("yes".equals(overtime.getAct().getFlag())?"[重申] ":"[销毁] ")+overtime.getAct().getComment());
			
			// 完成流程任务
			Map<String, Object> vars = Maps.newHashMap();
			vars.put("pass", "yes".equals(overtime.getAct().getFlag())? "1" : "0");
			actTaskService.complete(overtime.getAct().getTaskId(), overtime.getAct().getProcInsId(), overtime.getAct().getComment(),overtime.getContent(), vars);
		}
	}
	
	
	
	
	/**
	 * 审核审批保存
	 * @param t
	 */
	@Transactional(readOnly = false)
	public void auditSave(Overtime overtime) {
		
		// 设置意见
		overtime.getAct().setComment(("yes".equals(overtime.getAct().getFlag())?"[同意] ":"[驳回] ")+overtime.getAct().getComment());
		
		overtime.preUpdate();
		
		// 对不同环节的业务逻辑进行操作
		String taskDefKey = overtime.getAct().getTaskDefKey();

		// 审核环节
		if ("audit".equals(taskDefKey)){
			
		}

/**
TODO: overtime.getAct().getComment() 设值到 业务表，并且提交数据库
 */
		
		// 提交流程任务
		Map<String, Object> vars = Maps.newHashMap();
		vars.put("pass", "yes".equals(overtime.getAct().getFlag())? true : false);
		actTaskService.complete(overtime.getAct().getTaskId(), overtime.getAct().getProcInsId(), overtime.getAct().getComment(), vars);
		
	}
	
	public Overtime get(String id) {
		return super.get(id);
	}
	
	public List<Overtime> findList(Overtime overtime) {
		return super.findList(overtime);
	}
	
	public Page<Overtime> findPage(Page<Overtime> page, Overtime overtime) {
		return super.findPage(page, overtime);
	}
	
//	@Transactional(readOnly = false)
//	public void save(Overtime overtime) {
//		super.save(overtime);
//	}
	
	@Transactional(readOnly = false)
	public void delete(Overtime overtime) {
		super.delete(overtime);
	}
	
}